package com.leesche.zcsp.node.utils.web;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by kaider on 2016/9/21.
 */
public class PagingResult<T> implements Serializable
{
        /** Constant <code>KEY_DATA="rows"</code> */
        public static final String KEY_DATA = "rows";
        /** Constant <code>KEY_TOTAL="total"</code> */
        public static final String KEY_TOTAL = "total";

        private Boolean result; //结果
        private String info;    //结果信息
        private List<T> rows;   //记录列表
        private Integer total;  //记录条数
        private Integer page;   //当前页数

        /**
         * <p>Constructor for PagingResult.</p>
         *
         * @param records a {@link java.util.List} object.
         * @param total a {@link java.lang.Integer} object.
         * @param page a {@link java.lang.Integer} object.
         */
        public PagingResult(List<T> records, Integer total, Integer page)
        {
            this.result = true;
            this.info = "success";
            this.rows = (records != null ? records : new ArrayList<T>());
            this.total = total;
            this.page = page;
        }

        private PagingResult(String info)
        {
            this.result = false;
            this.info = info;
            this.total = 0;
            this.page = 1;
        }


        public static PagingResult failure(Throwable throwable)
        {
            return new PagingResult(throwable.getMessage());
        }

        public T firstRecord()
        {
            if(getRows().isEmpty())
            {
                return null;
            }
            return getRows().get(0);
        }

        public boolean hasNotRecord()
        {
            return getRows().isEmpty();
        }

        /**
         * <p>Getter for the field <code>total</code>.</p>
         *
         * @return a {@link java.lang.Integer} object.
         */
        public Integer getTotal()
        {
            return total;
        }

        /**
         * <p>Setter for the field <code>total</code>.</p>
         *
         * @param total a {@link java.lang.Integer} object.
         */
        public void setTotal(Integer total)
        {
            this.total = total;
        }

        /**
         * <p>Getter for the field <code>rows</code>.</p>
         *
         * @return a {@link java.util.List} object.
         */
        public List<T> getRows()
        {
            return rows;
        }

        /**
         * <p>Setter for the field <code>rows</code>.</p>
         *
         * @param rows a {@link java.util.List} object.
         */
        public void setRows(List<T> rows)
        {
            this.rows = rows;
        }

        /**
         * <p>Getter for the field <code>page</code>.</p>
         *
         * @return a {@link java.lang.Integer} object.
         */
        public Integer getPage()
        {
            return page;
        }

        /**
         * <p>Setter for the field <code>page</code>.</p>
         *
         * @param page a {@link java.lang.Integer} object.
         */
        public void setPage(Integer page)
        {
            this.page = page;
        }

        /**
         * <p>Getter for the field <code>result</code>.</p>
         *
         * @return a {@link java.lang.Boolean} object.
         */
        public Boolean getResult()
        {
            return result;
        }

        /**
         * <p>Setter for the field <code>result</code>.</p>
         *
         * @param result a {@link java.lang.Boolean} object.
         */
        public void setResult(Boolean result)
        {
            this.result = result;
        }

        /**
         * <p>Getter for the field <code>info</code>.</p>
         *
         * @return a {@link java.lang.String} object.
         */
        public String getInfo()
        {
            return info;
        }

        /**
         * <p>Setter for the field <code>info</code>.</p>
         *
         * @param info a {@link java.lang.String} object.
         */
        public void setInfo(String info)
        {
            this.info = info;
        }

        /** {@inheritDoc} */
        @Override
        public String toString()
        {
            return "PagingResult{" +
                    "result=" + result +
                    ", info='" + info + '\'' +
                    ", rows=" + rows +
                    ", total=" + total +
                    ", page=" + page +
                    '}';
        }
}
